﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using Orange.Mvc4App.Models;
using System.Web.Caching;

namespace Orange.Mvc4App.Services {

    public interface IProductBrandService : IBaseService<ProductBrand> {
        ProductBrand GetNext(int id);
        Product GetProduct(int id);
    }

    public class ProductBrandService : BaseService, IProductBrandService {
        public IList<ProductBrand> GetList(Func<ProductBrand, bool> filters) {
            if (filters != null)
                return db.ProductBrands.Where(filters).ToList();
            else
                return db.ProductBrands.ToList();
        }

        public ProductBrand Find(params object[] keys) {
            return db.ProductBrands.Find(keys);
        }

        public void Add(ProductBrand model) {
            db.ProductBrands.Add(model);
        }

        public void Update(ProductBrand model) {
            db.Entry(model).State = EntityState.Modified;
        }

        public void Delete(ProductBrand model) {
            db.ProductBrands.Remove(model);
        }

        public void Save() {
            db.SaveChanges();
        }

        public void Dispose() {
            db.Dispose();
        }

        public ProductBrand GetNext(int proid) {
            int id = 1;
            if (db.ProductBrands.Count() > 0)
                id = db.ProductBrands.Max(c => c.Id) + 1;
            return new ProductBrand { Id = id, Proid = proid };
        }

        public Product GetProduct(int id) {
            return db.Products.Find(id);
        }
    }
}